import dirImage from '@/assets/images/file/dir.svg';
import { memo } from 'react';
import { FileSvg, VideoFirstFrame } from '@/components/index.jsx';

/**
 * 图片文件展示
 *
 * @param props 属性
 * @param props.data 资源信息
 */
function FileImage(props) {
  const { data } = props;
  const { resourceType, mediaType } = data;
  const height = 80;

  if (mediaType && mediaType.startsWith('image/')) {
    return (
      <img
        alt="文件图"
        src={`/v/${data?.encryptionId}`}
        style={{ height, objectFit: 'cover', width: '100%' }}
        loading="lazy"
      />
    );
  } else if (resourceType === 'file') {
    // 区分文件媒体类型
    if (mediaType === 'application/pdf') {
      return <FileSvg height={height} text="PDF" />;
    } else if (mediaType.startsWith('audio/') || mediaType === 'application/ogg') {
      return <FileSvg height={height} text="音频" />;
    } else if (mediaType.startsWith('video/')) {
      return <VideoFirstFrame height={height} url={`/v/${data?.encryptionId}`} />;
    } else if (mediaType === 'text/plain') {
      return <FileSvg height={height} text="TXT" />;
    } else if (mediaType === 'text/css') {
      return <FileSvg height={height} text="CSS" />;
    } else if (mediaType === 'text/javascript') {
      return <FileSvg height={height} text="JS" />;
    } else if (mediaType === 'text/html') {
      return <FileSvg height={height} text="HTML" />;
    } else if (mediaType === 'application/json') {
      return <FileSvg height={height} text="JSON" />;
    } else if (mediaType === 'application/xml') {
      return <FileSvg height={height} text="XML" />;
    } else if (mediaType === 'application/zip' || mediaType === 'application/x-zip-compressed') {
      return <FileSvg height={height} text="ZIP" />;
    } else if (mediaType === 'application/x-msdownload') {
      return <FileSvg height={height} text="EXE" />;
    } else {
      return <FileSvg height={height} text="文件" />;
    }
  } else if (resourceType === 'directory') {
    return <img alt="文件图" src={dirImage} style={{ height }} loading="lazy" />;
  }

  return <></>;
}

export default memo(FileImage);
